package org.eclipseuseradmin.internal.ui.handlers;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.IWizard;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.handlers.HandlerUtil;

public abstract class BaseOpenWizardHandler extends AbstractHandler {

	public Object execute(ExecutionEvent event) throws ExecutionException {

		WizardDialog dialog = new WizardDialog(getActiveShell(event),
				getWizard(event));
		dialog.create();
		dialog.open();

		return null;
	}

	protected abstract IWizard getWizard(ExecutionEvent event);

	protected IStructuredSelection getCurrentSelection(ExecutionEvent event) {

		ISelection selection = HandlerUtil.getCurrentSelection(event);
		if (selection instanceof IStructuredSelection) {
			return (IStructuredSelection) selection;
		}

		return null;
	}

	protected IWorkbench getWorkbench(ExecutionEvent event) {
		return HandlerUtil.getActiveSite(event).getWorkbenchWindow()
				.getWorkbench();
	}

	private Shell getActiveShell(ExecutionEvent event) {
		return HandlerUtil.getActiveShell(event);
	}

}
